package com.nmgs.util.zip;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UtilFile {
    //检查slip漏洞
    public static final File newFileWithCheck(final String destinationDirName, final String name) throws IOException {
        File destinationDir = new File(destinationDirName);

        return newFileWithCheck(destinationDir, name);
    }

    public static final File newFileWithCheck(final File destinationDir, final String name) throws IOException {

        File destFile = new File(destinationDir, name);
        String destDirPath = destinationDir.getCanonicalPath();
        String destFilePath = destFile.getCanonicalPath();

        if (!destFilePath.startsWith(destDirPath + File.separator)) {
            throw new IOException("Entry is outside of the target dir: " + name);
        }
        return destFile;
    }

    public final static void copyNoClose(InputStream in, OutputStream out) throws IOException {

//        byte[] buffer = new byte[BufferSize << 1];
        int BufferSize = 0;
        byte[] buffer = new byte[BufferSize << 1];

        int bytesRead;

        while ((bytesRead = in.read(buffer)) != -1) {

            out.write(buffer, 0, bytesRead);

        }
        out.flush();
    }

    public static void main(String[] args) {
        File[] roots = File.listRoots();
        List<Map<String, Object>> DIskList = new ArrayList<>();
        for (File file : roots) {
            Map<String, Object> DISKMap = new HashMap<>();
            DISKMap.put("DIskUseing", (file.getTotalSpace() - file.getUsableSpace()) / 1024 / 1024 / 1024);
            DISKMap.put("DIskUNUseing", file.getUsableSpace() / 1024 / 1024 / 1024);
            DISKMap.put("DIskTotal", file.getTotalSpace() / 1024 / 1024 / 1024);
            DISKMap.put("DISKADDR", file.getPath());
            DIskList.add(DISKMap);
        }
        System.out.println(DIskList.toString());
    }
}
